class Result {
  static CODE = {
    SUCCESS: 200,
    FAIL: 400,
    ERROR: 500,
  };

  static MSG = {
    SUCCESS: '操作成功',
    FAIL: '操作失败',
    ERROR: '操作异常',
  };

  static STATUS = {
    SUCCESS: 'success',
    FAIL: 'fail',
    ERROR: 'error',
  };

  constructor(data, msg, code) {
    this.data = data;
    this.msg = msg || Result.MSG[Result.getStatusByCode(code)];
    this.code = code || Result.CODE.SUCCESS;
  }

  static success(data, MSG) {
    return new Result(data, MSG, Result.CODE.SUCCESS);
  }

  static fail(data, MSG) {
    return new Result(data, MSG, Result.CODE.FAIL);
  }

  static error(data, MSG) {
    return new Result(data, MSG, Result.CODE.ERROR);
  }

  static getStatusByCode(code) {
    switch (code) {
      case Result.CODE.SUCCESS:
        return Result.STATUS.SUCCESS;
      case Result.CODE.FAIL:
        return Result.STATUS.FAIL;
      case Result.CODE.ERROR:
        return Result.STATUS.ERROR;
      default:
        return Result.STATUS.SUCCESS;
    }
  }
}

module.exports = Result;